//
//  DBManager.m
//  Puzzle_Crazy
//
//  Created by VIETHUNG on 4/2/14.
//  Copyright (c) 2014 DanielLe. All rights reserved.
//

#import "DBManager.h"
#import "FMDatabase.h"
#import "PuzzleObject.h"
#import "CategoryObject.h"

static DBManager *dbManager = nil;
static FMDatabase *database;
@implementation DBManager

+ (DBManager *)shareInstance
{
    if (!dbManager) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            dbManager = [[DBManager alloc] init];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *docsPath = [paths objectAtIndex:0];
            NSString *path = [docsPath stringByAppendingPathComponent:@"Puzzle.sqlite"];
            database = [FMDatabase databaseWithPath:path];
            
        });
    }
    return dbManager;
}

- (void)insertData:(PuzzleObject *)puzzleObj
{
    [database open];
    [database executeUpdate:[NSString stringWithFormat:kinsertDataDB,puzzleObj.Objimage,puzzleObj.Objeasy,puzzleObj.Objmedium,puzzleObj.Objhard]];
    
    [database close];
}

- (void)updateEasyData:(PuzzleObject *)puzzleObj
{
    [database open];
    [database executeUpdate:[NSString stringWithFormat:kupdateEasyDataDB,puzzleObj.Objeasy,puzzleObj.Objimage]];
    
    [database close];
}

- (void)updateMediumData:(PuzzleObject *)puzzleObj
{
    [database open];
    [database executeUpdate:[NSString stringWithFormat:kupdateMediumDataDB,puzzleObj.Objmedium,puzzleObj.Objimage]];
    
    [database close];
}

- (void)updateHardData:(PuzzleObject *)puzzleObj
{
    [database open];
    [database executeUpdate:[NSString stringWithFormat:kupdateHardDataDB,puzzleObj.Objhard,puzzleObj.Objimage]];
    
    [database close];
}

- (void)updateLevelData:(PuzzleObject *)puzzleObj
{
    [database open];
    [database executeUpdate:[NSString stringWithFormat:kupdateLevelDataDB,puzzleObj.Objeasy,puzzleObj.Objmedium,puzzleObj.Objhard,puzzleObj.Objimage]];
    
    [database close];
}

- (NSMutableArray*)showDataPuzzle:(int)idPu
{
    NSMutableArray *arrayData = [NSMutableArray array];
    [database open];
    FMResultSet *results = [database executeQuery:[NSString stringWithFormat:kshowDataDBPu,idPu]];
    while ([results next]) {
        
        PuzzleObject *puzzleObject = [[PuzzleObject alloc] init];
        puzzleObject.Objimage   = [results stringForColumn:kImageDB];
        puzzleObject.Objeasy    = [results stringForColumn:kEasyDB];
        puzzleObject.Objmedium  = [results stringForColumn:kMediumDB];
        puzzleObject.Objhard    = [results stringForColumn:kHardDB];
        
        [arrayData addObject:puzzleObject];
    }
    [database close];
    return arrayData;
}

- (NSMutableArray*)showDataCategory
{
    NSMutableArray *arrayData = [NSMutableArray array];
    [database open];
    FMResultSet *results = [database executeQuery:kshowDataDBCa];
    while ([results next]) {
        
        CategoryObject *caObject = [[CategoryObject alloc] init];
        caObject.Objid          = [[results stringForColumn:kId] intValue];
        caObject.Objcategory    = [results stringForColumn:kCategory];
        
        [arrayData addObject:caObject];
    }
    [database close];
    return arrayData;
}

@end
